package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService{
    
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void deleteById(Long id) {

        roleMapper.deleteRelation(id);

        roleMapper.deleteById(id);
    }

    @Override
    public void insert(Role role, Long[] ids) {
        roleMapper.insert(role);

        if (ids != null && ids.length > 0) {
            roleMapper.insertBatchRelation(role.getId(), ids);
        }
    }

    @Override
    public Role selectById(Long id) {
        return roleMapper.selectById(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void updateById(Role role, Long[] ids) {
        roleMapper.updateById(role);

        roleMapper.deleteRelation(role.getId());

        if (ids != null && ids.length > 0) {
            roleMapper.insertBatchRelation(role.getId(), ids);
        }
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        //  查询满足条件的每页显示的数据
        List<Role> roles = roleMapper.queryForList(qo);
        //  返回一个结果集
        return new PageInfo<>(roles);
    }
}
